import { getCarInfo } from '@/api'
import type { blockMode } from '@/stores/block'
import { useCarStore } from '@/stores/car'
import { ref, type Ref } from 'vue'

let st: NodeJS.Timeout

// 底部抽屉是否可见
export const detailDrawerVisible = ref(false)
// 状态数据
export const statusData: Ref<Record<string, any> & { obstacleMode?: blockMode }> = ref({})

export const getDetailDrawer = async (value: boolean) => {
  // 抽屉关闭时停止定时获取数据
  if (!value) {
    clearTimeout(st)
    return
  }
  updateData()
}

export async function updateData() {
  clearTimeout(st)
  const carStore = useCarStore()
  const res = await getCarInfo(carStore.currentCar!)
  statusData.value = res.data
  st = setTimeout(updateData, 1000)
}
